package com.smedi.ismedi.inmail.core.application.query.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smedi.ismedi.inmail.core.application.command.AppVersionCheckVersionCmd;
import com.smedi.ismedi.inmail.core.application.command.AppVersionQuery;
import com.smedi.ismedi.inmail.core.application.convert.AppConverter;
import com.smedi.ismedi.inmail.core.application.query.AppVersionQueryService;
import com.smedi.ismedi.inmail.core.application.query.vo.CheckVersionVo;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.AppVersion;
import com.smedi.ismedi.inmail.core.domain.repository.AppVersionRepository;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.model.AppVersionEntity;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.mybatis.mapper.AppVersionMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @className: AppVersionQueryServiceImpl
 * @Description: TODO
 * @version: v1.0.0
 * @author: wzx
 * @date: 2023/8/30 10:40
 */

@Service
@AllArgsConstructor
@Slf4j
public class AppVersionQueryServiceImpl implements AppVersionQueryService {

    private final AppVersionRepository appVersionRepository;

    @Override
    public AppVersion get(AppVersionQuery appVersionQuery) {
        return appVersionRepository.get(appVersionQuery);
    }

    @Override
    public List<AppVersion> list(AppVersionQuery appVersionQuery) {
        return appVersionRepository.list(appVersionQuery);
    }

    @Override
    public Page<AppVersion> page(AppVersionQuery appVersionQuery) {
        return appVersionRepository.page(appVersionQuery);
    }

    @Override
    public CheckVersionVo checkVersion(AppVersionCheckVersionCmd appVersionCheckVersionCmd) {
        List<AppVersion> appVersions = appVersionRepository.list(AppVersionQuery.builder()
                .enable(true)
                .systemType(appVersionCheckVersionCmd.getSystemType())
                .versionGt(appVersionCheckVersionCmd.getLocalVersion())
                .build());

        if (CollectionUtils.isEmpty(appVersions)) {
            return CheckVersionVo.builder()
                    .version(appVersionCheckVersionCmd.getLocalVersion())
                    .needUpdate(false)
                    .build();
        }

        AppVersion development = appVersions.stream()
                .sorted(Comparator.comparing(AppVersion::getVersionOne)
                        .thenComparing(AppVersion::getVersionTwo)
                        .thenComparing(AppVersion::getVersionThree)
                        .reversed())
                .collect(Collectors.toList()).get(0);

        return CheckVersionVo.builder()
                .version(development.getAppVersion())
                .content(development.getContent())
                .address(development.getAddress())
                .method(appVersions.stream().map(AppVersion::getMethod).collect(Collectors.toList()).contains("apk") ? "apk" : "wgt")
                .needUpdate(true)
                .build();
    }
}